package physicsModeler.objects;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import physicsModeler.PhysXProcessor;

public class Anvil extends DrawObject {
	
private double scaleFactor;
private int sizeScaleX, sizeScaleY;
	
	public Anvil()
	{
		redValue = 128;
		yellowValue = 128;
		greenValue = 128;		
		sizeX = 25;
		sizeY = 25;
		sizeScaleX = 1;
		sizeScaleY = 1;
		scaleFactor = 2.5;
		// Still working on physics implementation
		// physics = new PhysXProcessor(positionX, positionY);
	}
	
	public Anvil(int positionX, int positionY)
	{
		this.positionX   = positionX;
		this.positionY   = positionY;
		// physics = new PhysXProcessor(positionX, positionY);
		redValue = 128;
		yellowValue = 128;
		greenValue = 128;
		
	}
	
	public Anvil(int positionX, int positionY, int sizeX, int sizeY)
	{		
		this.positionX   = positionX;
		this.positionY   = positionY;
		this.sizeX       = sizeX/25;
		this.sizeY       = sizeY/25;
		redValue = 128;
		yellowValue = 128;
		greenValue = 128;
		// physics = new PhysXProcessor(positionX, positionY);
	}
	
	public Anvil(int positionX, int positionY, double initialSpeed)
	{
		
		this.positionX   = positionX;
		this.positionY   = positionY;
		// physics = new PhysXProcessor(positionX, positionY, radius);
		// physics.setDx(initialSpeed);
		redValue = 128;
		yellowValue = 128;
		greenValue = 128;
	}

	@Override
	public void draw(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		
	    AffineTransform saveTransform = g2d.getTransform();  // save current transform 
	    AffineTransform scaleMatrix = new AffineTransform();  // create a new transform for scaling a polygon
	    
	    RenderingHints hints = new RenderingHints(
	                                  RenderingHints.KEY_ANTIALIASING,
	                                  RenderingHints.VALUE_ANTIALIAS_ON);
	    g2d.addRenderingHints(hints);
	    Color color = new Color(redValue,greenValue,yellowValue);
		g2d.setColor(color);
	    Polygon a = new Polygon();
	    
		int xCoord[] = {positionX, positionX+185, positionX+185, positionX+210, positionX+115, positionX+115, positionX+140, positionX+140, 
				positionX+40, positionX+40, positionX+65, positionX+65, positionX};
		int yCoord[] = {positionY, positionY, positionY+5, positionY+5, positionY+50, positionY+60, positionY+80, positionY+95, positionY+95,
				positionY+80, positionY+60, positionY+50, positionY+15};
	    
	    for (int i=0; i<xCoord.length; i++){
	    	a.addPoint(xCoord[i], yCoord[i]);	    	
	    }
	    
	    
	    scaleMatrix.scale((sizeScaleX/scaleFactor), (sizeScaleY/scaleFactor)); // scale the new transform according to the sizeX and sizeY fields
	    Rectangle r = a.getBounds();
	    scaleMatrix.translate(r.x * (scaleFactor-1), r.y * (scaleFactor-1)); // resets the location of the scaled shape back to its original x and y coordinates
	    g2d.setTransform(scaleMatrix);  // apply the scaled transform
  
	    g.fillPolygon(a);  // draw polygon on scaled transform
	    g2d.setTransform(saveTransform); // restores original transform
	}
	
	public String getName()
	{
		return "Anvil";	
	}
	
	@Override
	public void setSizeX(int sizeX) {
		//this.sizeX = sizeX;
		this.sizeX = sizeX;
		setSizeScaleX(sizeX/25);
	}
	
	@Override
	public void setSizeY(int sizeY) {
		//this.sizeY = sizeY;
		this.sizeY = sizeY;
		setSizeScaleY(sizeY/25);
	}
		
	public int getSizeScaleX() {
		return sizeScaleX;
	}

	public void setSizeScaleX(int sizeScaleX) {
		this.sizeScaleX = sizeScaleX;
	}

	public int getSizeScaleY() {
		return sizeScaleY;
	}

	public void setSizeScaleY(int sizeScaleY) {
		this.sizeScaleY = sizeScaleY;
	}

	public void setScaleFactor(double scaleFactor) {
		this.scaleFactor = scaleFactor;
	}
	
	public double getScaleFactor() {
		return scaleFactor;
	}
	
}
